package cn.gson.fengboot.controller;

import cn.gson.fengboot.aspect.MyLog;
import cn.gson.fengboot.model.mapper.ZorderMapper;
import cn.gson.fengboot.model.pojos.*;
import cn.gson.fengboot.model.service.*;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
public class TfyClientController {
    @Autowired
    TfyClientService tfyClientService;

    @Autowired
    ZcontractService ddservice;

    @Autowired
    ZorderService zorderService;

    @Autowired
    ZreturnedmoneyService zreturnedmoneyService;

    @Autowired
    ZreturnedgoodService zreturnedgoodService;

    @Autowired
    ZbarterService zbarterService;

    @Autowired
    ZdeliveryService zdeliveryService;

    @Autowired
    ZchukuService zchukuService;

    @Autowired
    ZdeliverylogService zdeliverylogService;

    @Autowired
    ZinvoiceService zinvoiceService;

    @Autowired
    ZreturnedmoneylogService zreturnedmoneylogService;

    @Autowired
    ZrukuService zrukuService;

    @Autowired
    LkefuService lkefuService;

    @Autowired
    LtousuService ltousuService;

    @Autowired
    LwxzbService lwxzbService;

    @Autowired
    TfyrelationwarnService tfyrelationwarnService;

    @Autowired
    ZorderMapper zorderMapper;

    @RequestMapping("client-list")
    @ResponseBody
    public Map<String,Object> clientList(Integer page,Integer size,String search,String search1){
        System.out.println(page);
        System.out.println(size);
        //客户分页查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allClient = tfyClientService.findAllClient(search,search1);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allClient);
        return map;
    }

    @RequestMapping("ygclient-list")
    @ResponseBody
    public Map<String,Object> ygclientList(Integer page,Integer size,String search,Integer empId){
        System.out.println(page);
        System.out.println(size);
        //根据员工id查询客户、模糊查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allClient = tfyClientService.findAllygClient(search,empId);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allClient);
        return map;
    }

    @RequestMapping("/cxidemp-list")
    @ResponseBody
    public QempFrom cxidemp(Integer empId){
        //根据员工id查询员工信息
        QempFrom qemp = tfyClientService.cxidemp(empId);
        return qemp;
    }

//    @RequestMapping("/cxclientname-list")
//    @ResponseBody
//    public TfyclientForm cxclientname(String clientName){
//        //查询客户名称是否相同
//        Integer tfyclientForm = tfyClientService.cxclientname(clientName);
//        return tfyclientForm;
//    }

    @RequestMapping("clientgz-list")
    @ResponseBody
    public Map<String,Object> clientgzList(Integer page,Integer size,String search){
        System.out.println(page);
        System.out.println(size);
        //已关注客户分页查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allClient = tfyClientService.findAllClientgz(search);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allClient);
        return map;
    }

    @RequestMapping("clientzt-list")
    @ResponseBody
    public Map<String,Object> clientztList(Integer page,Integer size,String search){
        System.out.println(page);
        System.out.println(size);
        //已失效客户分页查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allClient = tfyClientService.findAllClientzt(search);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allClient);
        return map;
    }

    @RequestMapping("/clientid-list")
    @ResponseBody
    public TfyclientForm clientidList(Integer clientId){
        //客户id查询
        System.out.println(clientId);
        TfyclientForm tfyclientForm = tfyClientService.findIdClient(clientId);
        return tfyclientForm;
    }

    @RequestMapping("clientdate-list")
    @ResponseBody
    public Map<String,Object> ClientDateList(Integer page,Integer size){
        System.out.println(page);
        System.out.println(size);
        //今日需联系客户分页查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allData = tfyClientService.findClientDate();
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allData);
        return map;
    }

    @RequestMapping("meclient-list")
    @ResponseBody
    public Map<String,Object> ClientList(Integer page,Integer size,String search){
        System.out.println(page);
        System.out.println(size);
        //分配给我的客户查询
        Page<Object> obj = PageHelper.startPage(page, size);
        List<TfyclientForm> allclient = tfyClientService.findClient(search);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allclient);
        return map;
    }

    @PostMapping("/addclient-list")
    @ResponseBody
    public Integer addclient(String busfrom){
        JSONObject obj = (JSONObject)JSONObject.parse(busfrom);
        TfyclientForm kh=obj.getObject("jh",TfyclientForm.class);
        String mc = obj.getObject("mc", String.class);
        //新增客户方法
        return tfyClientService.addclient(kh,mc);
    }

    @PostMapping("/addclienter-list")
    @ResponseBody
    public Integer addclienter(@RequestBody TfyclientForm kh){
        //新增客户方法
        return tfyClientService.addclienter(kh);
    }

    @PostMapping("/addkhlxr-list")
    @ResponseBody
    public void addclientlxr(Integer khid){
        //新增默认客户联系人方法
        tfyClientService.addclientlxr(khid);
    }

    @RequestMapping("dclient-list")
    @ResponseBody
    public void deleteClients(String khdx){
        //客户物理删除
        tfyClientService.deleteClient(khdx);
    }

    @RequestMapping("dclients-list")
    @ResponseBody
    public Integer khsc(String khdx){
        //单行，批量删除客户对象
        return tfyClientService.khsc(khdx);
    }

    @RequestMapping("clienthf-list")
    @ResponseBody
    public void khhf(String khdx){
        //单行，批量恢复客户状态
        tfyClientService.khhf(khdx);
    }

    /*客户id查询跟进记录*/
    @RequestMapping("/selectAllTails-list")
    public List<Dtail> selectAllTails(Integer clientId){
        return tfyClientService.selectAllTails(clientId);
    }

    //根据客户id查询商机
    @RequestMapping("/findclientsj-list")
    public List<Dbusiness> findclientsj(Integer clientId){
        return tfyClientService.findclientsj(clientId);
    }

    //根据客户id or id查询商机
    @RequestMapping("/findclientsjtwo-list")
    public List<Dbusiness> findclientsjtwo(Integer clientId,Integer clientId2){
        return tfyClientService.findclientsjtwo(clientId,clientId2);
    }

    @PostMapping("/gjclient-list")
    @ResponseBody
    public void gjztClient(String khdx){
        //修改客户跟进状态
        tfyClientService.gjztClient(khdx);
    }

    @PostMapping("/jgzclient-list")
    @ResponseBody
    public void jgzclient(String khdx){
        //单行 批量 加关注客户时修改 状态为1
        tfyClientService.jgzclient(khdx);
    }

    @PostMapping("/ygzclient-list")
    @ResponseBody
    public void ygzclient(String khdx){
        //单行 批量 已关注客户时修改 状态为0
        tfyClientService.ygzclient(khdx);
    }

    @RequestMapping("/findemp-list")
    @ResponseBody
    public Map<String,Object> findemps(Integer page,Integer size,String search){
        //查询员工和对应部门
        Page<Object> obj = PageHelper.startPage(page, size);
        List<QempFrom> allemp = tfyClientService.findemp(search);
        //封装数据
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",allemp);
        return map;
    }

    @RequestMapping("/yg-list")
    @ResponseBody
    public List<QempFrom> findemps(String search){
        //查询员工和对应部门
        return tfyClientService.findemp(search);
    }

    @RequestMapping("/cxempname-list")
    @ResponseBody
    public QempFrom cxempname(String empName){
        //<!--根据导入负责人名称查询员工名称-->
        return tfyClientService.cxempname(empName);
    }

    @RequestMapping("/findditu-list")
    public List<TfyclientForm> findditu(){
        //客户地图报表
        return tfyClientService.findditu();
    }

    @RequestMapping("/findbingz-list")
    public List<TfyclientForm> findbingz(){
        //客户饼状图报表 客户行业
        return tfyClientService.findbingz();
    }

    @RequestMapping("/findbingzer-list")
    public List<TfyclientForm> findbingzer(){
        //客户饼状图报表 客户来源
        return tfyClientService.findbingzer();
    }

    @RequestMapping("/findbingzsan-list")
    public List<TfyclientForm> findbingzsan(){
        //客户饼状图报表 客户级别
        return tfyClientService.findbingzsan();
    }

    @RequestMapping("/findbingzsi-list")
    public List<TfyclientForm> findbingzsi(){
        //客户饼状图报表 客户类型
        return tfyClientService.findbingzsi();
    }

    @RequestMapping("/findzhuz-list")
    public List<TfyclientForm> findzhuz(){
        //柱状图
        return tfyClientService.findzhuz();
    }

    //合同查询方法
    @RequestMapping("/ht-list")
    public List<Zcontract> hetonlist(String search,String search2){
        return ddservice.hetonlist(search,search2);
    }

    //客户全部视图
    @RequestMapping("/allselect-list")
    public List<Tfyselectvo> allselect(Integer clientId){
        return tfyClientService.allselect(clientId);
    }

    //交付计划z
    @RequestMapping("/khjfjh-list")
    public List<Zdelivery> jfjh(Integer id){
        System.err.println("id:"+id);
        return tfyClientService.jfjh(id);
    }

    //<!--谭方圆=>客户视图的查询合同详情-->
    @RequestMapping("hetongs-list")
    public List<Zcontract> hetonglist(Integer id){
        return tfyClientService.hetonglist(id);
    }

//    <!--谭方圆=>跳订单详情查询-->
    @RequestMapping("dingdans-list")
    public List<Zorder> ddlists(Integer id){
        return tfyClientService.ddlists(id);
    }

    //<!--谭方圆=>客户查询回款计划详情-->
    @RequestMapping("huikuan-list")
    public List<Zreturnedmoney> huikuanlists(Integer id){
        return tfyClientService.huikuanlists(id);
    }

    //<!--谭方圆=>跳退货查询详情-->
    @RequestMapping("tuihuo-list")
    public List<Zreturnedgood> tuihuolists(Integer id){
        return tfyClientService.tuihuolists(id);
    }

    //<!--谭方圆=>跳换货查询详情-->
    @RequestMapping("huanhuo-list")
    public List<Zbarter> huanhuolists(Integer id){
        return tfyClientService.huanhuolists(id);
    }

    @PostMapping("/clienthb-list")
    @ResponseBody
    public void hebinglist(String busform){
        System.out.println(busform);
        //客户合并
        tfyClientService.hebinglist(busform);
    }

    //<!--谭方圆=>客户合并的合同查询-->
    @RequestMapping("tfyheton-list")
    public List<Zcontract> tfyhetonlist(Integer id1,Integer id2){
        return ddservice.tfyhetonlist(id1, id2);
    }

    //<!--谭方圆=>客户合并的订单查询-->
    @RequestMapping("tfydd-list")
    public List<Zorder> tfyddlists(Integer id1,Integer id2){
        return zorderService.tfyddlists(id1, id2);
    }

    //<!--谭方圆=>客户合并查询回款计划-->
    @RequestMapping("tfyhuikuan-list")
    public List<Zreturnedmoney> tfyhuikuanlists(Integer id1,Integer id2){
        return zreturnedmoneyService.tfyhuikuanlists(id1, id2);
    }

    //<!--谭方圆=>客户合并查询退货-->
    @RequestMapping("tfytuihuo-list")
    public List<Zreturnedgood> tfytuihuolists(Integer id1,Integer id2){
        return zreturnedgoodService.tfytuihuolists(id1, id2);
    }

    //<!--谭方圆=>客户合并查询换货-->
    @RequestMapping("tfyhuanhuo-list")
    public List<Zbarter> tfyhuanhuolists(Integer id1,Integer id2){
        return zbarterService.tfyhuanhuolists(id1, id2);
    }

    //<!--谭方圆=>客户合并查询交付计划-->
    @RequestMapping("tfyjfjh-list")
    public List<Zdelivery> tfyjfjh(Integer id1,Integer id2){
        return zdeliveryService.tfyjfjh(id1, id2);
    }

    //<!--谭方圆=>客户合并查询出库-->
    @RequestMapping("tfychukuliebiao-list")
    public List<Zchuku> tfychukuliebiao(Integer id1,Integer id2){
        return zchukuService.tfychukuliebiao(id1, id2);
    }

    //<!--谭方圆=>客户合并查询出库-->
    @RequestMapping("tfykaipiao-list")
    public List<Zdeliverylog> tfykaipiao(Integer id1,Integer id2){
        return zdeliverylogService.tfykaipiao(id1, id2);
    }

    //<!--谭方圆=>客户合并查询发票-->
    @RequestMapping("tfyfapiao-list")
    public List<Zinvoice> tfyfapiao(Integer id1,Integer id2){
        return zinvoiceService.tfyfapiao(id1, id2);
    }

    //<!--谭方圆=>客户合并查询发票-->
    @RequestMapping("tfyhueikuanjl-list")
    public List<Zreturnedmoneylog> tfyhueikuanjl(Integer id1,Integer id2){
        return zreturnedmoneylogService.tfyhueikuanjl(id1, id2);
    }

    //<!--谭方圆=>客户合并查询入库-->
    @RequestMapping("tfyruku-list")
    public List<Zruku> tfyruku(Integer id1,Integer id2){
        return zrukuService.tfyruku(id1, id2);
    }

    //<!--谭方圆=>客户合并查询客户服务-->
    @RequestMapping("tfyfuwu-list")
    public List<Lkefu> tfyfuwu(Integer id1,Integer id2){
        return lkefuService.tfyfuwu(id1, id2);
    }

    //<!--谭方圆=>客户合并查询客户投诉-->
    @RequestMapping("tfytousu-list")
    public List<Ltousu> tfytousu(Integer id1,Integer id2){
        return ltousuService.tfytousu(id1, id2);
    }

    //<!--谭方圆=>客户合并查询维修工单-->
    @RequestMapping("tfyweixiu-list")
    public List<Lwxzb> tfyweixiu(Integer id1,Integer id2){
        return lwxzbService.tfyweixiu(id1, id2);
    }

    //<!--谭方圆=>客户合并查询联系提醒-->
    @RequestMapping("khhbrelationwarn-list")
    public List<TfyrelationwarnFrom> khhbrelationwarn(Integer id1,Integer id2){
        return tfyrelationwarnService.khhbrelationwarn(id1, id2);
    }

}